<?php
/**********************
**intellistrip.inc  //Should be intellistrip_actions.inc
**Date Created: 4/13/2009
**Copyright 2009 Tomorrow Tech Industries
**Contact: RPITomorrowTech@gmail.com
**********************/
/*Had to move to bottom
require_once("errors.inc");
require_once("intellistrip_xml_rpc.inc");
*/
define('INTELLISTRIP_FORM_ID', 'intellistripDeviceId');
define('INTELLISTRIP_FORM_PORT_ID', 'intellistripDevicePortId');

//All ports tag
define('INTELLISTRIP_PORT_ALL', 0);
//Minimum port number
define('INTELLISTRIP_PORT_BASE', 1);
//How many ports are there?
define('INTELLISTRIP_NUMBER_PORTS', 5);

define('INTELLISTRIP_POWER_OFF', 0);
define('INTELLISTRIP_POWER_ON', 100);


/**********
Base class interface for the server and client
Client will implement these as XML-RPC sends
Server will have an XML-RPC wrapper around this that calls these
**********/
class IntellistripActions
{	
	/*********
	Private members
	********/
	
	private static $debugging = false;

	//A string, for debugging
	private $lastError;
	
	//Password to the Intellistrip
	private $serverPassword;

	/********
	Initialization
	********/
	
	//Constructor
	public function IntellistripActions()
	{
		$this->setServerPassword('intellimation');
	}
	
	public function getLastError()
	{
		return $this->lastError;
	}

	public function clearLastError()
	{
		$this->lastError = '';
	}

	protected function setLastError($lastError)
	{
		$this->lastError = $lastError;
	}

	/********
	(XML-RPC) functions
	********/

	/********************
	Device power
	********************/
	/********************
	Set device power
	portID: portID of plug to access
	powerLevel: percentage of power to put out
	********************/
	public function setPortPower($portID, $powerLevel)
	{
		return TT_UNSUPPORTED;
	}

	//Get device power
	//
	public function getPortPower($portID, &$powerLevel)
	{
		return TT_UNSUPPORTED;
	}

	/********************
	Misc
	********************/
	//Query device information
	//Note this is NOT password protected
	/*******************
	vendorName: vendor supplied name.
		modelNumber: a number unique with respect to a vendor name to identify 
			different products 
		serialNumber: a number unique to an individual device when combined
			with the vendor name and model number
		numberPorts: the number of ports availible on the device
		state: is the power strip on and availible?
	*******************/
	public function getIntellistripInformation(&$vendorName, &$modelNumber, 
									&$modelVersion, &$serialNumber,
									&$numberPorts, &$state)
	{
		return TT_UNSUPPORTED;
	}
		
	/**********************
	//Sets the local authentication password
	public function setPassword($newPassword)
	{
		if( !$this->isValidPassword($newPassword) )
		{
			return TT_INVALID;
		}
		$this->password = $newPassword;
		return TT_OK;
	}
	
	//Gets the local authentication password
	public function getPassword(&$password)
	{
		$password = $this->password;
		return TT_OK;
	}
	**********************/
	
	//changePassword
	//newPassword: what Intellistrip's authentication password will be set to if succeeded
	public function changePassword($newPassword)
	{
		return TT_UNSUPPORTED;
	}		

	/***********
	Utility (local) functions
	***********/
	
	//SQL rules etc may make some passwords invalid
	public function isPasswordFormatCorrect($password)
	{
		return TT_OK;
	}

	//These get a bit iffy when the DB layer is used
	public function setServerPassword($serverPassword)
	{
		$this->serverPassword = $serverPassword;
	}

	public function getServerPassword()
	{
		return $this->serverPassword;
	}

	public static function setDebugging($debugging)
	{
		IntellistripActions::$debugging = $debugging;
	}

	public static function getDebugging()
	{
		return IntellistripActions::$debugging;
	}
}

/*
A sample unit test function
Note that this should NOT be inside the class, unless it is static
	TODO: can we have static PHP class code?
*/
function testIntellistrip()
{
	//Did ALL tests run successfully?
	$succeeded = true;
	
	echo "<H1>IntellistripActions Testing</H1>\n";

	$strip = new IntellistripActions();
	
	/*
	//Test: intial password should be blank
	echo "<H2>Initial password</H2>\n";
	if( TT_FAILED($strip->getPassword($password)) )
	{
		echo "<B>FAILED</B>: could not get password\n";
		$succeeded = false;
	}
	echo "
		Initial password: " . $password . "\n"
		. "<BR>\n";
	if( $password != '' )
	{
		echo "
			<B>FAILED</B>: expected blank\n
			<BR>\n";
		$succeeded = false;
	}
	else
	{
		echo "
			Succeeded\n
			<BR>\n";
	}
	echo "<BR>\n";
	*/

	//Test: changing the password should result in the same password back
	/*
	echo "<H2>Change password</H2>\n";
	$passwordNew = "rhennzelehr";
	echo "Changing to: " . $passwordNew . "
			<BR>\n";
	if( TT_FAILED($strip->setPassword($passwordNew)) )
	{
		echo "<B>FAILED</B>: could not set password
			<BR>\n";
		$succeeded = false;
	}
	if( TT_FAILED($strip->getPassword($passwordFetched)) )
	{
		echo "<B>FAILED</B>: could not get password
			<BR>\n";
		$succeeded = false;
	}
	echo "Set to: " . $passwordFetched . "
		<BR>";
	if( $passwordNew != $passwordFetched )
	{
		echo "<B>FAILED</B>: expected " . $passwordNew . "
			<BR>\n";
		$succeeded = false;
	}
	else
	{
		echo "Succeeded
			<BR>\n";
	}
	echo "<BR>\n";
	*/

	return $succeeded;
}
require_once("errors.inc");
require_once("intellistrip_xml_rpc.inc");
?>
